package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.service.BizService;

@Controller
@RequestMapping(value = "/student")
public class AddVideoStudyRecordController extends BaseController {
	
	@Autowired
	private BizService bizService;

	@Autowired
	private BizDao bizDao;
	
	private static Logger logger = LoggerFactory.getLogger(AddVideoStudyRecordController.class);

	/**
	 * 视频学习记录接口（所有用户）
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/addVideoRecord", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String addVideoRecord(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");
		
		try {
			//用户ID
			String userId = request.getParameter("userId");
			//学员ID
			String studentId = request.getParameter("studentId");
			//视频ID
			String videoId = request.getParameter("videoId");
			//科目
			String subjectId = request.getParameter("subjectId");
			//学驾类型
			String driverType = request.getParameter("driverType");
			//电子教学日志ID
			String studyResultId = request.getParameter("studyResultId");
			if((null != userId && !userId.isEmpty())
					&& (null != driverType && !driverType.isEmpty())
					&& (null != videoId && !videoId.isEmpty())
					&& (null != subjectId && !subjectId.isEmpty())){
				Map<String, Object> valMap = new HashMap<String, Object>();
				valMap.put("userId", Integer.parseInt(userId));
				valMap.put("driverType", driverType);
				valMap.put("videoId", videoId);
				valMap.put("subjectId", Integer.parseInt(subjectId));
				valMap.put("studentId", studentId);
				valMap.put("studyResultId", studyResultId);
				//添加视频学习记录
				response = bizService.addVideoRecord(valMap,response);	
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填参数!");
			}
		} catch (Exception e) {
			logger.info("addVideoRecord："+ e.toString());
			response.setErrorcode(1);
			response.setMessage("添加学时记录异常！");
		} finally {
			return response.toJSON();
		}

	}
	
	
	/**
	 * 视频学习学时接口（已线下绑定的学员）
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/addStudyRecord", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String addStudyRecord(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");
		
		try {
			//学时类型
			String type = request.getParameter("type");
			//学员编号ID
			String studentId = request.getParameter("studentId");
			//学驾类型
			String driverType = request.getParameter("driverType");
			//开始时间
			String beginTime = request.getParameter("beginTime");
			//结束时间
			String endTime = request.getParameter("endTime");
			//学习时长
			String studyTime = request.getParameter("studyTime");
			//科目
			String subjectId = request.getParameter("subjectId");
			//驾校编号
			String driveSchoolId = request.getParameter("driveSchoolId");
			//驾校名称
			String driveSchoolName = request.getParameter("driveSchoolName");
			//课程编码
			String trainingPrograms = request.getParameter("currId");
			//点击视频播放时的时间戳
			String classId = request.getParameter("classId");
			//学员全国编号
			String stuNum = request.getParameter("stuNum");
			//培训机构编号
			String inscode = request.getParameter("inscode");
			
			if((null != studentId && !studentId.isEmpty())
					&& (null != driverType && !driverType.isEmpty())
					&& (null != beginTime && !beginTime.isEmpty())
					&& (null != endTime && !endTime.isEmpty())
					&& (null != studyTime && !studyTime.isEmpty())
					&& (null != subjectId && !subjectId.isEmpty())
					&& (null != driveSchoolId && !driveSchoolId.isEmpty())
					&& (null != driveSchoolName && !driveSchoolName.isEmpty())
					&& (null != classId && !classId.isEmpty())
					&& (null != stuNum && !stuNum.isEmpty())
					&& (null != inscode && !inscode.isEmpty())
					&& (null != type && !type.isEmpty())){
				DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
				Map<String, Object> valMap = new HashMap<String, Object>();
				valMap.put("studentId", Integer.parseInt(studentId));
				valMap.put("beginTime", beginTime);
				valMap.put("endTime", endTime);
				valMap.put("studyTime", Float.parseFloat(studyTime));
				valMap.put("subjectId", Integer.parseInt(subjectId));
				valMap.put("driveSchoolId", Integer.parseInt(driveSchoolId));
				valMap.put("stuNum", stuNum);
				valMap.put("driveSchoolName", driveSchoolName);
				valMap.put("trainingPrograms", trainingPrograms);
				valMap.put("classId", classId);
				if(type.equals("1")){//视频学时
					valMap.put("studyType", "4");
					LocalDateTime ldt = LocalDateTime.parse(beginTime,df);
					//学时时长*系数+开始时间beginTime
					long minutes =(long) (Float.parseFloat(studyTime) * Integer.parseInt(Config.getStudyRatio()));
					valMap.put("endTime", df.format(ldt.plusMinutes(minutes)));
					valMap.put("studyTime", minutes);
				}else if(type.equals("2")){//模考学时
					valMap.put("studyType","5");
				}
				//添加视频学习记录
				response = bizService.addStudyResult(valMap,response);	
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填参数!");
			}
 		} catch (Exception e) {
			logger.info("addStudyRecord："+ e.toString());
			response.setErrorcode(1);
			response.setMessage("添加学时记录异常！");
		} finally {
			return response.toJSON();
		}

	}

}
